package com.example.java_gobang.Controller;

import com.example.java_gobang.common.AjaxResult;
import com.example.java_gobang.common.AppVariable;
import com.example.java_gobang.common.PasswordUtils;
import com.example.java_gobang.common.UserSessionUtils;
import com.example.java_gobang.model.User;
import com.example.java_gobang.model.UserMapper;
import com.fasterxml.jackson.databind.DatabindContext;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request,HttpSession session,String verifyCode,String username, String password){
        if(username == null || password == null){
            return AjaxResult.fail(-1,"参数异常");
        }

        // 验证码查询
        String KaptchaCode = session.getAttribute("verifyCode") + "";
        if(StringUtils.isEmpty(KaptchaCode) || !(verifyCode.toLowerCase()).equals(KaptchaCode)){
            session.setAttribute("errorMsg","验证码错误");
            return AjaxResult.fail(-2,"验证码错误");
        }
        //用户名正确否
        User user = userMapper.selectByName(username);
        if(user == null){
            return AjaxResult.fail(-1,"用户不存在");
        }

        //账户状态
        if(user.getState() == -1){
            return AjaxResult.success(-3,"密码错误", 5);
        }

        //密码正确否
        if(PasswordUtils.check(password, user.getPassword())){
            session = request.getSession(true);
            session.setAttribute(AppVariable.USER_SESSION_KEY,user);
            // 清零错误次数
            userMapper.unlockUser(username);
            // 隐藏密码
            user.setPassword("");
            return AjaxResult.success(1);
        }else {
            userMapper.addErr(username);
            // 锁定账户
            int count = userMapper.rCount(username);
            if(count >= 5){
                userMapper.lockUser(username);
            }
            return AjaxResult.success(-3,"密码错误", count);
        }

    }

    @RequestMapping("/reg")
    public AjaxResult reg(User user){
        if(user == null || user.getUsername() == null || user.getPassword() == null){
            return AjaxResult.fail(-1,"参数异常");
        }
        user.setPassword(PasswordUtils.encrypt(user.getPassword()));
        userMapper.insert(user);
        userMapper.reg1(user.getUsername());
        return AjaxResult.success(1);
    }

    @RequestMapping("/getuser")
    public AjaxResult getUser(HttpServletRequest request){
        User user = UserSessionUtils.getUser(request);
        if(user == null){
            return AjaxResult.fail(-1,"参数异常");
        }
        User newUser = userMapper.selectByName(user.getUsername());
        return AjaxResult.success(newUser);
    }

    // 实现登录功能的验证码
    @GetMapping("/kaptcha")
    public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/gif");

        // 三个参数分别为宽、高、位数
        SpecCaptcha captcha = new SpecCaptcha(150, 30, 4);

        // 设置字体
        captcha.setCharType(Captcha.FONT_9);

        // 验证码存入session
        request.getSession().setAttribute("verifyCode", captcha.text().toLowerCase());

        // 输出图片流
        captcha.out(response.getOutputStream());

    }

}
